package com.zxkj.commonpatrol.adapt;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.daimajia.swipe.SwipeLayout;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.GravityEnum;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.model.entity.HistoryQuestEntity;
import com.zxkj.commonpatrol.model.response.DevEvtQuestPatrolHistoryResponse;
import com.zxkj.commonpatrol.ui.AddExceptionActivity;
import com.zxkj.commonpatrol.ui.ShowMediaActivity;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class HistoryQuestAdapt extends BaseQuickAdapter<DevEvtQuestPatrolHistoryResponse.DevEvtQuestHis, BaseViewHolder> {
    public HistoryQuestAdapt(int layoutResId, @Nullable List<DevEvtQuestPatrolHistoryResponse.DevEvtQuestHis> data) {
        super(layoutResId, data);
    }
    public MyCheckPointListCallBack mDataCallBack;
    public OnCallPhoneCallBack onCallPhoneCallBack;
    //数据回调接口
    public interface MyCheckPointListCallBack<T> {
        void onDataSuccessCallBack( T t);
        void onDataFailCallBack(T t,T t1);
    }
    public MyUnCheckPointListCallBack mUnDataCallBack;

    public MyJSCheckPointListCallBack mJSDataCallBack;

    public void setmJSDataCallBack(MyJSCheckPointListCallBack mJSDataCallBack) {
        this.mJSDataCallBack = mJSDataCallBack;
    }

    public void setmUnDataCallBack(MyUnCheckPointListCallBack<String> mUnDataCallBack) {
        this.mUnDataCallBack = mUnDataCallBack;
    }

    //数据回调接口
    public interface MyUnCheckPointListCallBack<T> {
        void onDataSuccessCallBack( T t,T t1);
        void onDataFailCallBack(T t,T t1);
    }

    //数据回调接口
    public interface MyJSCheckPointListCallBack<T> {
        void onDataSuccessCallBack( T t,T t1);
        void onDataFailCallBack(T t,T t1);
    }
    //电话回调
    public interface OnCallPhoneCallBack{
        void onCallPhone(String number);
    }

    public void setOnCallPhoneCallBack(OnCallPhoneCallBack onCallPhoneCallBack) {
        this.onCallPhoneCallBack = onCallPhoneCallBack;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, @Nullable DevEvtQuestPatrolHistoryResponse.DevEvtQuestHis historyQuestEntity) {
        SwipeLayout swipeLayout= baseViewHolder.itemView.findViewById(R.id.swipe);
        swipeLayout.setClickToClose(true);
        View viewById2 = baseViewHolder.itemView.findViewById(R.id.cd_xiufu);
        View viewById3 = baseViewHolder.itemView.findViewById(R.id.cd_uncheck);
        View viewById4 = baseViewHolder.itemView.findViewById(R.id.cd_js);
        TextView viewById = baseViewHolder.itemView.findViewById(R.id.tv_upcoming_name);
        ImageView viewById1 = baseViewHolder.itemView.findViewById(R.id.tv_num);




        LayoutInflater inflater = LayoutInflater.from(getContext());

        LinearLayout llAddView = baseViewHolder.itemView.findViewById(R.id.ll_add_view);
        llAddView.removeAllViews();
           if (historyQuestEntity.IsChild!=1){
               if (historyQuestEntity.getQItemOptionList()!=null){
                   if (historyQuestEntity.getQItemOptionList().size()>0){
                       for (int i = 0; i < historyQuestEntity.getQItemOptionList().size(); i++) {
//                          textView12.setText(checkPointListEntity.getDevices().getDevPropertys().get(i).Name);
//                          textView13.setText(checkPointListEntity.getDevices().getDevPropertys().get(i).Value);
//                          TextView textView =new TextView(getContext());
//                          textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,0,1));
//                          textView.setText("测试");

                           ConstraintLayout layout = (ConstraintLayout) inflater.inflate(
                                   R.layout.item_quest_detail_add_view, null).findViewById(R.id.cl);

                           ImageView imageView13 = (ImageView) layout.getViewById(R.id.imageView3);
                           if (historyQuestEntity.getQItemOptionList().get(i).getType()==1){
                               imageView13.setImageResource(R.drawable.date_d);
                           }else if (historyQuestEntity.getQItemOptionList().get(i).getType()==2){
                               imageView13.setImageResource(R.drawable.action_b);
                           }else{
                               imageView13.setImageResource(R.drawable.action_a);
                           }
                           TextView textView12 = (TextView) layout.getViewById(R.id.textView12);
                           TextView textView13 = (TextView) layout.getViewById(R.id.textView13);
                           textView12.setText(historyQuestEntity.getQItemOptionList().get(i).QOptionName + ":");
                           textView13.setText(historyQuestEntity.getQItemOptionList().get(i).InputValue);

                           llAddView.addView(layout);


                       }
                   }
               }
           }













        if (historyQuestEntity.getResultType()==0){
            viewById2.setVisibility(View.VISIBLE);
            viewById3.setVisibility(View.VISIBLE);
            viewById4.setVisibility(View.VISIBLE);
            swipeLayout.setSwipeEnabled(true);
            viewById.setTextColor(getContext().getColor(R.color.red));
        }else{

            viewById2.setVisibility(View.GONE);
            viewById3.setVisibility(View.GONE);
            viewById4.setVisibility(View.VISIBLE);


            swipeLayout.setSwipeEnabled(true);
            viewById.setTextColor(getContext().getColor(R.color.green));
        }
        if (historyQuestEntity.IsExistsMediaData==1){
            viewById1.setVisibility(View.VISIBLE);
        }else{
            viewById1.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(historyQuestEntity.getActionNo())){
            baseViewHolder.itemView.findViewById(R.id.imageView6).setVisibility(View.GONE);
            baseViewHolder.itemView.findViewById(R.id.textView14).setVisibility(View.GONE);
        }else{
            baseViewHolder.itemView.findViewById(R.id.imageView6).setVisibility(View.VISIBLE);
            baseViewHolder.itemView.findViewById(R.id.textView14).setVisibility(View.VISIBLE);
        }
        viewById1.setOnClickListener(v->{
            //跳转到查看视频语音

            Intent intent =new Intent(getContext(), ShowMediaActivity.class);
            if (historyQuestEntity.getResultDesc()!=null){
                intent.putExtra("name",historyQuestEntity.getResultDesc());
                intent.putExtra("time",historyQuestEntity.getLastPatrolDateTime());
            }

            if (historyQuestEntity.IsChild==1){
                intent.putExtra("AlarmId",historyQuestEntity.getSubAlarmId());
            }else{
                intent.putExtra("AlarmId",historyQuestEntity.getLogId());
            }



            getContext().startActivity(intent);



        });
        baseViewHolder.setText(R.id.tv_upcoming_name,historyQuestEntity.getResultDesc());
        baseViewHolder.setText(R.id.tv_address,historyQuestEntity.getLastPatrolUser());
        baseViewHolder.setText(R.id.tv_address2,historyQuestEntity.getActionNo());
        baseViewHolder.setText(R.id.tv_to_address,historyQuestEntity.getLastPatrolDateTime());
        swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
            @Override
            public void onStartOpen(SwipeLayout layout) {
                baseViewHolder.itemView.findViewById(R.id.cl).setEnabled(false);
            }

            @Override
            public void onOpen(SwipeLayout layout) {
                baseViewHolder.itemView.findViewById(R.id.cl).setEnabled(false);
            }

            @Override
            public void onStartClose(SwipeLayout layout) {

            }

            @Override
            public void onClose(SwipeLayout layout) {
                baseViewHolder.itemView.findViewById(R.id.cl).setEnabled(true);
            }

            @Override
            public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {

            }

            @Override
            public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {

            }
        });


        baseViewHolder.itemView.findViewById(R.id.rl_Delete).setOnClickListener(v -> {

            new MaterialDialog.Builder(getContext())
                    .content("是否修復?")
                    .contentGravity(GravityEnum.CENTER)
                    .negativeColor(getContext().getResources().getColor(R.color.colorPrimary))
                    .positiveColor(getContext().getColor(R.color.colorPrimary))
                    .buttonsGravity(GravityEnum.CENTER)
                    .positiveText("是")
                    .negativeText("否")
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                            ActivityTool.getInstance().finishAllActivities();
//                          ARouter.getInstance().build("/Login/LoginActivity").navigation(getContext().getApplicationContext());

                            swipeLayout.close();

                            //修复
                            mDataCallBack.onDataSuccessCallBack(historyQuestEntity.getLogId());
                        }
                    })
                    .onNegative((v1,v2)->{
                        swipeLayout.close();
                    })
                    .show();

        });

        baseViewHolder.itemView.findViewById(R.id.rl_uncheck).setOnClickListener(v->{

            new MaterialDialog.Builder(getContext())
                    .content("是否確認?")
                    .contentGravity(GravityEnum.CENTER)
                    .negativeColor(getContext().getResources().getColor(R.color.colorPrimary))
                    .positiveColor(getContext().getColor(R.color.colorPrimary))
                    .buttonsGravity(GravityEnum.CENTER)
                    .positiveText("是")
                    .negativeText("否")
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                            ActivityTool.getInstance().finishAllActivities();
//                          ARouter.getInstance().build("/Login/LoginActivity").navigation(getContext().getApplicationContext());

                            swipeLayout.close();
                            //未修复
                            mUnDataCallBack.onDataSuccessCallBack(historyQuestEntity.getLogId(),historyQuestEntity.getResultDesc());
                        }
                    }


                    )
                    .onNegative(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            swipeLayout.close();
                        }
                    })
                    .show();


        });


        baseViewHolder.itemView.findViewById(R.id.rl_js).setOnClickListener(v->{
            swipeLayout.close();
            mJSDataCallBack.onDataSuccessCallBack(historyQuestEntity.getLogId(),historyQuestEntity.getResultDesc());
        });


        if (TextUtils.isEmpty(historyQuestEntity.LastPatrolUserPhone)){
            baseViewHolder.itemView.findViewById(R.id.iv_call).setVisibility(View.GONE);
        }else{
            baseViewHolder.itemView.findViewById(R.id.iv_call).setVisibility(View.VISIBLE);
        }


        baseViewHolder.itemView.findViewById(R.id.iv_call).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCallPhoneCallBack!=null){
                    onCallPhoneCallBack.onCallPhone(historyQuestEntity.getLastPatrolUserPhone());
                }
            }
        });
    }
    public void setmDataCallBack(MyCheckPointListCallBack<String> mDataCallBack) {
        this.mDataCallBack = mDataCallBack;
    }
}
